#!/bin/sh
which vim
case $? in
     0 )
         editor=vim
         echo " You do have vim installed, well done! "
	 ;;
     * )
         editor=vi
         echo " You don't have vim installed, sorry! "
         ;;
esac

which uboot-envedit
case $? in
     0 )
         env_editor="uboot-envedit"
         echo " You do have uboot-envedit installed, well done! "
	 ;;
     * )
         which envedit.pl
	 case $? in
	      0 )
                  env_editor="envedit.pl"
		  echo " You don't have uboot-envedit installed, sorry! "
		  echo " Try installing fso-utils package. "
		  echo " I will use envedit.pl instead! "
		  ;;
              * )
                  echo " You don't have uboot-envedit nor envedit.pl installed, sorry! "
		  echo " Try installing fso-utils package. "
		  exit 1
		  ;;
         esac
esac

cat /dev/mtd2 > uboot.env
cp uboot.env uboot.env.bak
echo " ${env_editor} -i uboot.env -p > uboot.env.txt "
${env_editor} -i uboot.env -p > uboot.env.txt
$editor uboot.env.txt
echo " ${env_editor} -i uboot.env -f uboot.env.txt -o uboot.env.new "
${env_editor} -i uboot.env -f uboot.env.txt -o uboot.env.new
echo " ${env_editor} -i uboot.env.new -p > uboot.env.new.txt "
${env_editor} -i uboot.env.new -p > uboot.env.new.txt
$editor uboot.env.txt

echo ' Was that OK? [y/n] '
read ceck
case $ceck in 
  y )
    echo ' Very well... '
    echo ' flash_eraseall /dev/mtd2 '
    flash_eraseall /dev/mtd2
    echo ' nandwrite -p /dev/mtd2 uboot.env.new '
    nandwrite -p /dev/mtd2 uboot.env.new
    ;;
  * )
    echo ' nothing more to do, bye bye :-) '
    ;;
esac
